package xuecheng.tool.validation;

import java.util.Set;

import javax.validation.*;
import javax.validation.groups.Default;

import org.springframework.util.ObjectUtils;

/**
 * 参数校验工具类
 *
 * @author PengXueCheng
 * @version 1.0.0
 */
public class ValidUtils {

    private static Validator validator;

    static {
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        validator = vf.getValidator();
    }

    /**
     * 参数验证
     *
     * @param object
     *            object to validate
     * @param groups
     *            the group or list of groups targeted for validation (defaults to * {@link Default})
     * @throws ConstraintViolationException
     */
    public static <T> void validate(T object, Class<?>... groups) throws ConstraintViolationException {
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(object, groups);
        if (ObjectUtils.isEmpty(constraintViolations)) {
            return;
        }
        throw new ConstraintViolationException(constraintViolations);
    }

}
